package com.yh.correct.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.yh.common.utils.ExcelUtil;
import com.yh.correct.model.Page;
import com.yh.correct.model.ResultData;
import com.yh.correct.model.VoLabour;
import com.yh.correct.service.IVoLabourService;

/**
 * 公益劳动登记控制层
 * 
 * @author 王鹏程 
 * @Date 2017.03.22
 * @Time 14:08
 */
@RestController
@RequestMapping("/voLabour")
public class VoLabourController {

	/**
	 * 日志记录
	 */
	private static Logger logger = LogManager.getLogger(VoLabourController.class);
	/**
	 * 注入公益劳动登记服务层接口
	 */
	@Autowired
	private IVoLabourService IVo;
	/**
	 * 公益劳动登记首页跳转
	 * 
	 * @return
	 */
	@RequestMapping(value="/skip")
	public ModelAndView skip(){ 
		return new ModelAndView("education/voLabour");
	}
	/**
	 * 考核人下拉框显示
	 *  
	 * @return
	 */
	@RequestMapping(value="/kselect")
	public List<Object> kShowSelect(){ 
		List<Object> list = IVo.getAllPersonnel();
		return list;
	}
	/**
	 * 增加窗口跳转
	 * 
	 * @return
	 */
	@RequestMapping(value="/addSkip")
	public ModelAndView addSkip(){ 
		return new ModelAndView("education/voLabour_add");
	}
	/**
	 * 增加公益劳动登记
	 * 
	 * @param voLabour
	 * @return
	 */
	@RequestMapping(value="/add")
	public ResultData addVoLabour(VoLabour voLabour){
		int i = IVo.addVoLabour(voLabour);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("增加成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("增加失败！");
		}
		logger.debug("增加公益劳动登记!");
		return rd;
	}
	/**
	 * 查看单条详细公益劳动登记
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/look")
	public ModelAndView getbVoLabour(int id){ 
		VoLabour voLabour = new VoLabour();
		voLabour.setId(id);
		Map<String,Object> map = IVo.getVoLabour(voLabour);
		logger.debug("查看单条详公益劳动登记!");
		return new ModelAndView("education/voLabour_look",map);
	}
	/**
	 * 修改前查询单条详细公益劳动登记
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/detail")
	public ModelAndView getaVoLabour(int id){ 
		VoLabour voLabour = new VoLabour();
		voLabour.setId(id);
		Map<String,Object> map = IVo.getVoLabour(voLabour);
		logger.debug("查看单条详公益劳动登记!");
		return new ModelAndView("education/voLabour_edit",map);
	}
	/**
	 * 修改公益劳动登记
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/edit")
	public ResultData editVoLabour(VoLabour voLabour){
		logger.debug(voLabour);
		int i = IVo.editVoLabour(voLabour);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("修改成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("修改失败！");
		}
		logger.debug("公益劳动登记!");
		return rd;
	}
	/**
	 * 删除公益劳动登记
	 * 
	 * @param voLabour
	 * @return
	 */
	@RequestMapping(value="/del")
	public ResultData delVoLabour(VoLabour voLabour){ 
		int i = IVo.delVoLabour(voLabour);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("删除成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("删除失败！");
		}
		logger.debug("删除公益劳动登记!");
		return rd;
	}
	/**
	 * 模糊查询公益劳动登记
	 * 
	 * @param name
	 * @param jname
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@RequestMapping(value="/getLike")
	public ResultData getLikeVoLabour(String name,String jname,@RequestParam int pageSize,@RequestParam int pageNum){
		Map<String,Object> condition=new HashMap<String,Object>();
		condition.put("pageSize", pageSize);
		condition.put("pageNum", pageNum);
		condition.put("name", name);
		condition.put("jname", jname);
		Page page = IVo.getLikeVoLabours(condition);
		ResultData rd=new ResultData();
		if(page!=null){
			List<Object> voLabour = new ArrayList<Object>();
			voLabour.add(page);
			rd.setCode(0);
			rd.setMessage("查询成功！");
			rd.setData(voLabour);
		}else{
			rd.setCode(-1);
			rd.setMessage("查询失败！");
		}
		logger.debug("公益劳动登记!");
		return rd;
	}
	/**
	 * 导出Excle
	 * 
	 * @param request
	 * @param response
	 * @param name
	 * @param jname
	 * @return
	 */
	@RequestMapping(value="/excel")
	public String outExcle(HttpServletRequest request, HttpServletResponse response,String name,String jname){
		Map<String,Object> condition=new HashMap<String,Object>();
		condition.put("jname", jname);
		condition.put("name", name);
		String fileName = "公益劳动登记文件";
		LinkedList<Map<String, Object>> projects = IVo.getAllVoLabours(condition);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sheetName", "sheet1");
		projects.add(0, map);
		String columnNames[] = { "公益劳动登记编号", "矫正单位", "姓名", "劳动日期", "劳动内容", "劳动时长", "表现情况" ,"基准分值","得分","考核人","备注"};
		String keys[] = { "id", "jname", "name", "date", "content", "time", "performance", "standard_score", "score", "dname","remarks" };
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			ExcelUtil.createWorkBook(projects, keys, columnNames).write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] content = os.toByteArray();
		InputStream is = new ByteArrayInputStream(content);
		response.reset();
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		try {
			response.setHeader("Content-Disposition",
					"attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(out);
			byte[] buff = new byte[2048];
			int bytesRead;
			while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
				bos.write(buff, 0, bytesRead);
			}
		} catch (final IOException e) {
			try {
				throw e;
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			if (bis != null)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (bos != null)
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return null;
	}
}
